<script setup>
import LimitFram from "./LimitFrame.vue";
import CustomerApi from "@/api/customerApi";
import { deepClone } from "@/utils";

const props = defineProps({
  limit: {
    type: Array,
    required: true,
  },
});

const blackGroupVisible = ref(false),
  form = ref({ exit: 1, groupId: [] }),
  blacklist = ref([]);

const formRef = ref();

const emits = defineEmits(["cancel"]);

const rules = {
  groupId: [{ required: true, message: "请选择会员等级", trigger: "change" }],
};

// 描述
const ruleText = computed(() => {
  let rule = props.limit.value;
  if (!rule.groupId) return "";
  return `${rule.exit == 1 ? "任意匹配" : "不匹配"} “${String(
    rule.groupName
  )}”`;
});

const queryVipMemInfo = async () => {
  const rs = await CustomerApi.queryVipMemInfo();
  if (rs.code == 200) {
    blacklist.value = rs.payload.map((item) => {
      return { value: item.id, label: item.memberName };
    });
  }
};

const getBlackKeys = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      props.limit.value = deepClone(form.value);
      formatLimit();
      blackGroupVisible.value = false;
    }
  });
};

// 生成表达式
const formatLimit = () => {
  // 表达式最终的样子
  // $_member.svip$$_1 == true

  // props.limit.propertyKey = "userGroup";
  // props.limit.propertyType = 0;
  // props.limit.businessType = "userGroup";
  // let sentencedToEmpty = `$_member.svip$$_${form.value.groupId.join("__")} != null &&`; // 表达式判空
  props.limit.keys = [
    {
      businessType: "member",
      key: `svip$$_${form.value.groupId.join("__")}`,
      obj: form.value.groupId
    },
  ];
  props.limit.expression = `$_member.svip$$_${form.value.groupId.join("__")}==${
    form.value.exit == 0 ? false : true
  }`;
  console.log('[  props.limit.expression ]',  props.limit.expression)
};

const addRule = () => {
  if (formRef.value) formRef.value.resetFields();
  blackGroupVisible.value = true;
};

const editRule = () => {
  form.value = deepClone(props.limit.value);
  blackGroupVisible.value = true;
};

const onClose = () => {
  emits("cancel");
};

const onChangeGroup = (val) => {
  let group = [];
  blacklist.value.forEach((item) => {
    if (val.includes(item.value)) {
      group.push(item.label);
    }
  });
  if (group.length) {
    form.value.groupName = group;
  }
};

onMounted(() => {
  queryVipMemInfo();
});

defineExpose({ add: addRule });
</script>
<template>
  <LimitFram title="会员等级" :content="ruleText" @edit="editRule" />
  <Dialog
    v-model="blackGroupVisible"
    title="会员等级"
    :width="500"
    @confirm="getBlackKeys"
    @close="onClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="类型" prop="exit">
        <el-radio-group v-model="form.exit">
          <el-radio :label="1">任意匹配</el-radio>
          <el-radio :label="0">不匹配 </el-radio>
          <!-- <el-radio :label="2">全匹配</el-radio> -->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="会员等级" prop="groupId">
        <el-select-v2
          v-model="form.groupId"
          :options="blacklist"
          filterable
          multiple
          placeholder="请选择会员等级"
          @change="onChangeGroup"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<style lang="scss" scoped>
.el-select-v2 {
  min-width: 70%;
}
</style>
